package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
/**
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)                 
 */

public class ReservationUI
{
    private ReservationCtr resvCtr;//DVD UI must know the DVD controller 
    
    public ReservationUI()
    {
        resvCtr = new ReservationCtr();    
    }
    
    public void start(ReservationCtr ctr)
    {
        resvCtr= ctr;
        reservationMenu();
    }
    
    public void start()
    {
        reservationMenu(); 
    }
    
    public void reservationMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise = writeReservationMenu();
            if (choise == 1)
            {
                createReservation();
            }
            else{
                if(choise == 2){
                   removeReservation();
                }
                else{
                    if(choise == 3){
                        listAllReservations();
                    }
                    else{
                        if(choise == 4){
                            reservationInfo();
                        }
                        else
                        {
                            exit = true;
                        }
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    private int writeReservationMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Reservation Menu ***");
            System.out.println(" (1) Create Reservation");
            System.out.println(" (2) Remove Reservation");
            System.out.println(" (3) List All Reservations");
            System.out.println(" (4) Reservation Info");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    
    private int inputResvNr()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Reservation Nr: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    private String inputResvDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Reservation Date: ");
        String resvDate = keyboard.nextLine();
        return resvDate;
    }
    
    private int inputPersonId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Person Id: ");
        int personId = keyboard.nextInt();
        return personId;
    }
    
    private int inputDvdId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Dvd Id: " );
        int loanStatus = keyboard.nextInt();
        return loanStatus;
    }
    
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
    
    public void createReservation()
    {
        int _resvId = inputResvNr();
        String _resvDate = inputResvDate();
        
        int personId = 0;
        String person = "";
        boolean personFound = false;
        while(!personFound)
        {
            personId = inputPersonId();
            
            person = resvCtr.getPersonInfo(personId);
            
            if(!person.isEmpty())
            {
                personFound = true;
            }
        }
        
        System.out.println(person);
        
        int dvdId = 0;
        String dvd = "";
        boolean dvdFound = false;
        while(!dvdFound)
        {
            dvdId = inputDvdId();
            
            dvd = resvCtr.getDvdInfo(dvdId);
            
            if(!dvd.isEmpty())
            {
                dvdFound = true;
            }
        }
        
        System.out.println(dvd);
        
        boolean success = resvCtr.createReservation(_resvId, _resvDate, personId, dvdId);
        
        System.out.println();
        if(success)
        {
            System.out.println("Reservation has been created!");
        }
        else
        {
            System.out.println("Reservation failed!");
        }
        
        pause();
    }
    
    public void removeReservation()
    {
        int resvId = inputResvNr();
        
        if(resvCtr.deleteReservation(resvId))
        {
            System.out.println("Delete succed!");
        }
        else
        {
            System.out.println("Could not find reservation");
        }
        
        pause();
    }
   
    public void listAllReservations()
    {
        String reservations = resvCtr.listAllReservations();
        
        if(reservations.isEmpty())
        {
            System.out.println("There are no reservations at this moment!");
        }
        else
        {
            System.out.println(reservations);    
        }
        pause();
    }
    
    public void reservationInfo()
    {
        String resv = resvCtr.getReservationInfoById(inputResvNr());
        
        if(resv.isEmpty())
        {
            System.out.println("Could not find reservation on that id!");
        }
        else
        {
            System.out.println(resv);    
        }
        
        pause();
    }
}
